package com.dog.basics.admin.controller.common;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import com.dog.basics.bean.dto.admin.sys.SysFileInfoAddDto;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.photooss.service.PhotoOssUtil;
import com.dog.basics.common.properties.FileProperties;
import com.dog.basics.common.util.FileNameUtils;
import com.dog.basics.common.util.FileUtils;
import com.dog.basics.common.util.R;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.service.common.IDogSysFileInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

/**
 * 文件上传 controller
 *
 * @author dog
 */
@RestController
@RequestMapping("/admin/file")
@AllArgsConstructor
@Api(value = SwaggerConstants.COMMON + "文件上传", tags = SwaggerConstants.COMMON + "文件上传")
@Log4j2
@Inner
public class FileController {

    private final FileProperties fileProperties;
    private final IDogSysFileInfoService dogSysFileInfoService;
    private final PhotoOssUtil photoOssUtil;
    /**
     * @Description: 上传图片(服务器)
     * @Param: file
     * @return: String
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
/*    @ApiOperation(value = "上传图片(服务器)", notes = "上传图片(服务器)", tags = SwaggerConstants.API_UPLOD)
    @PostMapping("/upload/element")
    public R<String> uploadElementFile(@RequestParam("file") MultipartFile file) {
        //  判断图片
        if (!file.isEmpty()) {
            try {
                return R.ok(photoOssUtil.uploadFile(file, null, null, null));
            } catch (Exception e) {
                log.error("上传失败文件:" + e.getMessage());
            }
        }
        return R.failed("上传失败");
    }*/
    /**
     * 上传图片
     *
     * @param file
     * @return guonh 2019年12月19日
     */
    @ApiOperation(value = "设备固件上传", notes = "设备固件上传")
    @PostMapping("/upload/deviceVersion")
    @ApiImplicitParam(name = "type", value = "0:图片 1:视频 2:音频 3其他", dataType = "Integer", required = true)
    @Inner
    public R<String> uploadDeviceVersion(@RequestParam("file") MultipartFile file, Integer type) {
        if (file.isEmpty()) {
            return R.failed();
        }
        SysFileInfoAddDto sysFileInfoAddDto = new SysFileInfoAddDto();
        String name = file.getOriginalFilename();
        String fileName = FileNameUtils.getFileName(FileNameUtils.getSuffix(file.getOriginalFilename()));
        String pagth = fileProperties.url();
        //判断上传是否成功
        if (Validator.isFalse(FileUtils.uploadDevice(file, pagth, name))) {
            return R.failed("上传失败");
        }
        //计算文件大小kb
        Integer kb = new BigDecimal(FileUtil.file(pagth + name).length())
                .divide(BigDecimal.valueOf(1024))
                .setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        sysFileInfoAddDto.setName(name);
        sysFileInfoAddDto.setFileName(fileName);
        sysFileInfoAddDto.setFilePath(pagth + fileName);
        sysFileInfoAddDto.setFileSuffix(FileNameUtils.getSuffix(file.getOriginalFilename()));
        sysFileInfoAddDto.setFileMdFileMdfive(DigestUtils.md5Hex(pagth + fileName));
        sysFileInfoAddDto.setFileSize(kb);
        sysFileInfoAddDto.setFileStatus(type);
        Validator.validateTrue(dogSysFileInfoService.saveObj(sysFileInfoAddDto), "上传失败");
        return R.ok(name);
    }


       /**
     * 上传图片
     *
     * @param file
     * @return guonh 2019年12月19日
     */
    @ApiOperation(value = "上传", notes = "上传")
    @PostMapping("/upload/element")
    @ApiImplicitParam(name = "type", value = "0:图片 1:视频 2:音频 3其他", dataType = "Integer", required = true)
    @Inner
    public R<String> uploadElementFile(@RequestParam("file") MultipartFile file, Integer type) {
        if (file.isEmpty()) {
            return R.failed();
        }
        SysFileInfoAddDto sysFileInfoAddDto = new SysFileInfoAddDto();
        String name = file.getOriginalFilename();
        String fileName = FileNameUtils.getFileName(FileNameUtils.getSuffix(file.getOriginalFilename()));
        String pagth = fileProperties.url();
        //判断上传是否成功
        if (Validator.isFalse(FileUtils.upload(file, pagth, fileName))) {
            return R.failed("上传失败");
        }
        //计算文件大小kb
        Integer kb = new BigDecimal(FileUtil.file(pagth + fileName).length())
                .divide(BigDecimal.valueOf(1024))
                .setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
        sysFileInfoAddDto.setName(name);
        sysFileInfoAddDto.setFileName(fileName);
        sysFileInfoAddDto.setFilePath(pagth + fileName);
        sysFileInfoAddDto.setFileSuffix(FileNameUtils.getSuffix(file.getOriginalFilename()));
        sysFileInfoAddDto.setFileMdFileMdfive(DigestUtils.md5Hex(pagth + fileName));
        sysFileInfoAddDto.setFileSize(kb);
        sysFileInfoAddDto.setFileStatus(type);
        Validator.validateTrue(dogSysFileInfoService.saveObj(sysFileInfoAddDto), "上传失败");
        return R.ok(fileName);
    }

    /**
     * 多文件上传
     *
     * @param file
     * @return guonh 2019年12月19日
     */
    @ApiOperation(value = "多文件上传", notes = "多文件上传")
    @PostMapping("/upload/elements")
    @ApiImplicitParam(name = "type", value = "0:图片 1:视频 2:音频 3其他", dataType = "Integer", required = true)
    @Inner
    public R<List<String>> uploadsElementFile(@RequestParam("file") MultipartFile[] file, Integer type) {
        Validator.validateTrue(file.length > 0, "请选择文件!");
        Validator.validateTrue(file.length <= 6, "最多允许一次上传6个文件!");
        SysFileInfoAddDto sysFileInfoAddDto = new SysFileInfoAddDto();
        List<String> fileNames = CollUtil.newArrayList();
        for (int i = 0; i < file.length; i++) {
            String name = file[i].getOriginalFilename();
            String fileName = FileNameUtils.getFileName(FileNameUtils.getSuffix(file[i].getOriginalFilename()));
            String pagth = fileProperties.url();
            //判断上传是否成功
            if (Validator.isFalse(FileUtils.upload(file[i], pagth, fileName))) {
                return R.failed("上传失败");
            }
            //计算文件大小kb
            Integer kb = new BigDecimal(FileUtil.file(pagth + fileName).length())
                    .divide(BigDecimal.valueOf(1024))
                    .setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
            sysFileInfoAddDto.setName(name);
            sysFileInfoAddDto.setFileName(fileName);
            sysFileInfoAddDto.setFilePath(pagth + fileName);
            sysFileInfoAddDto.setFileSuffix(FileNameUtils.getSuffix(file[i].getOriginalFilename()));
            sysFileInfoAddDto.setFileMdFileMdfive(DigestUtils.md5Hex(pagth + fileName));
            sysFileInfoAddDto.setFileSize(kb);
            sysFileInfoAddDto.setFileStatus(type);
            Validator.validateTrue(dogSysFileInfoService.saveObj(sysFileInfoAddDto), "上传失败");
            fileNames.add(fileName);
        }
        return R.ok(fileNames);
    }

}
